package com.example.qxfw.dysjrh.controller;

import cn.hutool.core.io.FileUtil;
import com.example.qxfw.common.SysResult;
import com.example.qxfw.dysjrh.config.DmgisConfig;
import com.example.qxfw.dysjrh.entity.ContourSetting;
import com.example.qxfw.dysjrh.entity.GridData;
import com.example.qxfw.dysjrh.entity.GridDataPicAndXY;
import com.example.qxfw.dysjrh.entity.QxGridFile;
import com.example.qxfw.dysjrh.service.GridService;
import com.example.qxfw.dysjrh.utils.GridUtil;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * @author wzt
 * @date 2025/01/02
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/grid")  // 格点数据查询 API 路径
public class GridController {
    private final GridService gridService;

    private final GridUtil gridUtil;

    /**
     * 获取格点数据
     * @param gridData 格点数据查询条件
     * @return 格点数据
     */
    @GetMapping("/getGridData")
    public SysResult getGridData(GridData gridData){
        Object result = gridService.getGridData(gridData);
        if (result == null ||
                result.equals("") ||
                (result instanceof String && ((String) result).length() == 0) ||
                (result instanceof Collection && ((Collection<?>) result).isEmpty())) {
            String result1 = "文件不存在";
            return SysResult.success(result1);
        }
        return SysResult.success(result);
//        return SysResult.success(gridService.getGridData(gridData));
    }



    //查询要素
    private static final Map<String, String> maps = new HashMap<>();

    static{
        maps.put("降水","ER03");
        maps.put("最高气温","TMAX");
        maps.put("最低气温","TMIN");
        maps.put("平均气温","TMP");
        maps.put("风速","EDA10");
    }

    private final DmgisConfig dmgisConfig;

    @RequestMapping(value = "/mapView")
    @ApiOperation("气象信息-精细化预报-gird成图")
    public SysResult mapView(GridData gridData) throws IOException {
//    public ResponseEntity<byte[]> mapView(GridData gridData) throws IOException {
        List<QxGridFile> qxGridFile1 = gridService.queryListByDateCharTimeCharNtimesType(gridData);

        if (qxGridFile1 == null || qxGridFile1.size() == 0) {
            String result1 = "文件不存在";
            return SysResult.success(result1);
        }

        List<String> list = new ArrayList<>();

        for (QxGridFile qxGridFile : qxGridFile1) {
            String name = gridData.getDateChar() + "_" + gridData.getTimeChar() + "_" + gridData.getNTimes() + "_" + gridData.getType();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_PNG); // 设置为图片的MIME类型
            headers.setContentDisposition(ContentDisposition.builder("attachment").filename(name + ".png").build());
            if (qxGridFile == null)
//                return new ResponseEntity<>(null, headers, HttpStatus.OK);
                return SysResult.success(list);
            String paramtype = "雨量";
            String type = gridData.getType();
            if (type.equals("ER03"))//雨
            {
                paramtype = "雨量";
            } else if (type.equals("ERH")) {
                paramtype = "湿度";
            } else if (type.equals("ECT")) {
                paramtype = "云量";
            } else if (type.equals("EDA10.U")) {
                paramtype = "风速";
            } else if (type.equals("TMP")) {
                paramtype = "温度";
            } else if (type.equals("最高气温")) {
                paramtype = "最高温度";
            } else if (type.equals("最低气温")) {
                paramtype = "最低温度";
            } else if (type.equals("平均气温")) {
                paramtype = "温度";
            } else if (type.equals("降水")){
                paramtype = "降水";
            } else if (type.equals("风速")) {
                paramtype = "风速";
            }

            List<ContourSetting> contourSetting = gridService.getContourSetting(paramtype);
            if (contourSetting == null || contourSetting.size() == 0) {
                String result1 = "文件不存在";
                return SysResult.success(result1);
            }

//        byte[] data = gridUtil.processGridDataPic(qxGridFile,contourSetting);
            GridDataPicAndXY gridDataPicAndXY = gridUtil.processGridDataPicAndXY(qxGridFile, contourSetting,dmgisConfig.getGridFilePath());

            if (gridDataPicAndXY == null) {
                String result1 = "文件不存在";
                return SysResult.success(result1);
            }

            byte[] filedata = gridDataPicAndXY.getFiledata();

            if (filedata == null || filedata.length == 0) {
                String result1 = "文件不存在";
                return SysResult.success(result1);
            }

            if (gridData == null || maps == null || !maps.containsKey(gridData.getType())) {
                String result1 = "数据或映射为空";
                return SysResult.success(result1);
            }

            String path2 = "DmgisPubData/dmgis_xjservice_file/grid_file"
                    + "/" + gridData.getDateChar() + "/" + gridData.getTimeChar() + "/" + gridData.getNTimes() + "/" + maps.get(gridData.getType())+ ".png";


            String fileName = dmgisConfig.getTxtFilePath() + path2;

            //删除文件
            FileUtil.del(fileName);
            //创建文件
            FileUtil.touch(fileName);
            FileUtil.writeBytes(filedata, new File(fileName));


            String imagePath = dmgisConfig.getNgfileUrl() + path2;

            list.add(imagePath);

//            headers.add("xMin", String.valueOf(gridDataPicAndXY.getxMin()));
//            headers.add("xMax", String.valueOf(gridDataPicAndXY.getxMax()));
//            headers.add("yMin", String.valueOf(gridDataPicAndXY.getyMin()));
//            headers.add("yMax", String.valueOf(gridDataPicAndXY.getyMax()));
//            return new ResponseEntity<>(gridDataPicAndXY.getFiledata(), headers, HttpStatus.OK);
        }
        return SysResult.success(list);
    }
    }




